package org.his.controller;

import java.util.Map;

import org.his.entity.Login;
import org.his.entity.Patient;
import org.his.entity.Prescription;
import org.his.entity.Registration;
import org.his.service.PatientService;
import org.his.service.PrescriptionService;
import org.his.service.RegistrationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

@SessionAttributes(value = { "prescription"})
@Controller
@RequestMapping("/prescription")
public class PrescriptionController {
	@Autowired
	private PatientService patientService;
	@Autowired
	private RegistrationService registrationService;
	@Autowired
	private PrescriptionService prescriptionService;
	
	@RequestMapping(value = "/checkpatient",produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String checkPatient(Registration cRegistration,ModelMap model) {	
		String stateString1 ="等待";
		if (stateString1.equals(cRegistration.getState())) {
			Patient patientinfo = patientService.checkPatient(cRegistration.getDiagnosticcard());
			Registration registrationinfo = registrationService.queryByMedicalrecord(cRegistration.getMedicalrecord());
			if (patientinfo != null && patientinfo.getName() != null) {						
				Prescription prescription =prescriptionService.setPrescription(registrationinfo, patientinfo);
				model.put("prescription", prescription);
					return "{\"success\":\"true\",\"message\":\"选择成功\"}";			
			} else
				return "{\"success\":\"false\",\"message\":\"选择失败\"}";
			
	}else {
		return "{\"success\":\"false\",\"message\":\"请选择待诊状态为等待1的病人新建病案\"}";		
	}
		
	}
	@RequestMapping(value = "/addpresciption", produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String addPresciption(Prescription addPrescription) {		
			int count = 0;
			int count1 = 0;
			count = prescriptionService.addPresciption(addPrescription);
			if (count == 1) {
				count1=registrationService.updateState(addPrescription.getMedicalrecord());
				return "{\"success\":\"true\",\"message\":\"新增处方成功，可在病人列表查询\"}";
			}else {
			return "{\"success\":\"false\",\"message\":\"新增处方失败，请检查是否完整\"}";
		}
	}
	
	@RequestMapping(value = "/queryprescription",produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String queryPrescription(Registration qRegistration,ModelMap model) {	
		String stateString1 ="已完成";
		if (stateString1.equals(qRegistration.getState())) {
			Prescription prescription = prescriptionService.queryByMedicalrecord(qRegistration.getMedicalrecord());
			if (prescription.getDiagnosis() != null) {										
				model.put("prescription", prescription);
					return "{\"success\":\"true\",\"message\":\"选择成功\"}";			
			} else
				return "{\"success\":\"false\",\"message\":\"选择失败\"}";
			
	}else {
		return "{\"success\":\"false\",\"message\":\"请选择待诊状态为已完成的病人查询病案\"}";		
	}
		
	}
	
}
